<style lang="scss" type="text/scss">
.ipt-w-name {
  width: 250px !important;
}
.ipt-w {
  width: 120px !important;
}
.preview-con {
  padding: 20px 0;
  display: flex;
  flex-wrap: wrap;
  div {
    flex: 0 0 33%;
  }
}
</style>

<template>
  <div>
    <el-form ref="templateForm" :inline="true" :rules="rules" :model="form" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="模板名称" prop="goodsTemplateName">
            <el-input class="ipt-w-name" v-model="form.goodsTemplateName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="开始时间" prop="timeStart">
        <el-time-select class="ipt-w" v-model="form.timeStart" :picker-options="{
          start: '08:30',
          step: '00:15',
          end: '23:30'
        }" placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-time-select class="ipt-w" v-model="form.endTime" :picker-options="{
          start: '08:30',
          step: '00:15',
          end: '23:30',
          minTime: form.timeStart
        }" placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="游戏场次">
        <el-input class="ipt-w" type="number" v-model="form.times"></el-input>
      </el-form-item>
      <el-form-item label="游戏时长/分钟">
        <el-input class="ipt-w" type="number" v-model="form.timeRange"></el-input>
      </el-form-item>
      <el-form-item label="每场间隔/分钟">
        <el-input class="ipt-w" type="number" v-model="form.interval"></el-input>
      </el-form-item>
    </el-form>
    <el-button @click="preview" type="primary">场次预览</el-button>
    <div class="preview-con">
      <div v-for="(item,index) in form.goodsTimes">
        <b>第{{index+1}}场:</b>
        <span>{{item.startTime}}-{{item.endTime}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
import 'moment/locale/zh-cn';

let defaultForm = {
  goodsTemplateName: '',
  timeStart: '',
  endTime: '',
  times: '',
  timeRange: '',
  interval: '',
  goodsTimes: []
};
export default {
  name: 'editForm',
  props: {
    formItem: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {},
      rules: {
        goodsTemplateName: [
          { required: true, message: '请输入模板名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        timeStart: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ],
        endTime: [{ required: true, message: '请选择时间', trigger: 'change' }]
      }
    };
  },
  watch: {
    formItem: {
      handler(formItem) {
        this.init();
      },
      deep: true
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.form = Object.assign({}, defaultForm, this.formItem);
    },
    preview() {
      let x = this.form.timeStart;
      let y = this.form.timeRange;
      let z = this.form.interval;
      let n = this.form.times;
      if (!x || !y || !z || !n) {
        this.$message({
          type: 'error',
          message: '请输入初始设置'
        });
        return false;
      }
      let arr = [];
      for (let i = 0; i < n; i++) {
        let obj = {};
        let str = '';
        let e = '';
        e = moment(x, 'HH:mm')
          .add(y, 'minutes')
          .format('HH:mm');
        obj.startTime = x;
        obj.endTime = e;
        arr.push(obj);
        x = moment(e, 'HH:mm')
          .add(z, 'minutes')
          .format('HH:mm');
      }
      this.form.goodsTimes = arr;
    }
  }
};
</script>
